package com.qianxx.base.utils;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.qianxx.draw.R;

public final class AlertUtils {

	private AlertUtils() {
	}

	public static AlertDialog alert(final Context context, int messagId) {
		return alert(context, messagId, null);
	}

	public static AlertDialog alert(final Context context, String message) {
		return alertView(context, null, null, message,
				context.getString(R.string.confirm_level), null, null, false,
				null);
	}

	public static AlertDialog alert(final Context context, int messagId,
			DialogInterface.OnClickListener onConfirmClickListener) {
		return alertView(context, null, null, context.getString(messagId),
				context.getString(R.string.confirm_level),
				context.getString(R.string.cancel_level),
				onConfirmClickListener, false, null);
	}

	public static AlertDialog alertView(Context context, View view,
			String title, String message, String confirm, String cancel,
			DialogInterface.OnClickListener onConfirmClickListener,
			boolean negavite,
			final DialogInterface.OnClickListener onCancelClickListener) {

		if (context instanceof Activity && ((Activity) context).isFinishing()) {
			return null;
		}

		final Builder builder = new AlertDialog.Builder(context);

		if (!TextUtils.isEmpty(title)) {
			builder.setTitle(title);
		}

		if (view != null) {
			builder.setView(view);
		}

		if (!TextUtils.isEmpty(message)) {
			builder.setMessage(message);
		}

		builder.setPositiveButton(confirm, onConfirmClickListener);

		if (negavite) {
			builder.setNegativeButton(cancel, onCancelClickListener);
			builder.setOnCancelListener(new DialogInterface.OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					if (onCancelClickListener != null) {
						onCancelClickListener.onClick(dialog, 0);
					}
				}
			});
		}

		final AlertDialog alert = builder.create();
		alert.show();

		return alert;
	}

	private static AlertDialog al;

	/**
	 * 
	 * @param mContext
	 * @param titleStringId
	 * @param onConfirmClickListener
	 * @return
	 */
	public static AlertDialog showDialog(final Context mContext,
			int titleStringId, OnClickListener onConfirmClickListener) {

		if (al != null) {
			al.cancel();
		}
		al = new AlertDialog.Builder(mContext).create();
		al.setCancelable(false);
		al.show();
		Window window = al.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = (int) (getWidth(mContext) * 0.85); // 宽度
		window.setAttributes(lp);
		window.setContentView(R.layout.base_alert_item);
		TextView alertTitle = (TextView) window.findViewById(R.id.alert_title);
		alertTitle.setText(mContext.getResources().getString(titleStringId));
		alertTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); // 指定单位，设置字体大小

		TextView cancelBtn = (TextView) window.findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				al.cancel();
			}
		});
		TextView comfirmBtn = (TextView) window.findViewById(R.id.comfirm_btn);
		comfirmBtn.setOnClickListener(onConfirmClickListener);
		return al;
	}

	// 确认 和 取消 都回调
	public static AlertDialog showDialog(final Context mContext,
			int titleStringId, OnClickListener onConfirmClickListener,
			OnClickListener onCancelClickListener) {

		if (al != null) {
			al.cancel();
		}
		al = new AlertDialog.Builder(mContext).create();
		al.setCancelable(false);
		al.show();
		Window window = al.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = (int) (getWidth(mContext) * 0.85); // 宽度
		window.setAttributes(lp);
		window.setContentView(R.layout.base_alert_item);
		TextView alertTitle = (TextView) window.findViewById(R.id.alert_title);
		alertTitle.setText(mContext.getResources().getString(titleStringId));
		alertTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); // 指定单位，设置字体大小

		TextView cancelBtn = (TextView) window.findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(onCancelClickListener);

		TextView comfirmBtn = (TextView) window.findViewById(R.id.comfirm_btn);
		comfirmBtn.setOnClickListener(onConfirmClickListener);
		return al;
	}

	public static AlertDialog showDialog2(final Context mContext,
			int titleStringId, OnClickListener onConfirmClickListener) {

		if (al != null) {
			al.cancel();
		}
		al = new AlertDialog.Builder(mContext).create();
		al.setCancelable(false);
		al.show();
		Window window = al.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = (int) (getWidth(mContext) * 0.85); // 宽度
		window.setAttributes(lp);
		window.setContentView(R.layout.base_alert_item);
		TextView alertTitle = (TextView) window.findViewById(R.id.alert_title);
		alertTitle.setText(mContext.getResources().getString(titleStringId));
		TextView cancelBtn = (TextView) window.findViewById(R.id.cancel_btn);
		cancelBtn.setVisibility(View.GONE);
		TextView comfirmBtn = (TextView) window.findViewById(R.id.comfirm_btn);
		comfirmBtn.setOnClickListener(onConfirmClickListener);
		return al;
	}

	public static AlertDialog showDialog(final Context mContext,
			int titleStringId, int cancelRes, int confirmRes,
			OnClickListener onConfirmClickListener) {

		if (al != null) {
			al.cancel();
		}
		al = new AlertDialog.Builder(mContext).create();
		al.setCancelable(false);
		al.show();
		Window window = al.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = (int) (getWidth(mContext) * 0.85); // 宽度
		window.setAttributes(lp);
		window.setContentView(R.layout.base_alert_item);
		TextView alertTitle = (TextView) window.findViewById(R.id.alert_title);
		alertTitle.setText(mContext.getResources().getString(titleStringId));
		alertTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); // 指定单位，设置字体大小
																// TODO

		TextView cancelBtn = (TextView) window.findViewById(R.id.cancel_btn);
		cancelBtn.setText(mContext.getResources().getString(cancelRes));
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				al.cancel();
			}
		});
		TextView comfirmBtn = (TextView) window.findViewById(R.id.comfirm_btn);
		comfirmBtn.setText(mContext.getResources().getString(confirmRes));
		comfirmBtn.setOnClickListener(onConfirmClickListener);
		return al;
	}

	public static AlertDialog showDialog(final Context mContext,
			String content, int cancelRes, int confirmRes,
			OnClickListener onConfirmClickListener) {

		if (al != null) {
			al.cancel();
		}
		al = new AlertDialog.Builder(mContext).create();
		al.setCancelable(false);
		al.show();
		Window window = al.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = (int) (getWidth(mContext) * 0.85); // 宽度
		window.setAttributes(lp);
		window.setContentView(R.layout.base_alert_item);
		TextView alertTitle = (TextView) window.findViewById(R.id.alert_title);
		alertTitle.setText(content);
		TextView cancelBtn = (TextView) window.findViewById(R.id.cancel_btn);
		cancelBtn.setText(mContext.getResources().getString(cancelRes));
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				al.cancel();
			}
		});
		TextView comfirmBtn = (TextView) window.findViewById(R.id.comfirm_btn);
		comfirmBtn.setText(mContext.getResources().getString(confirmRes));
		comfirmBtn.setOnClickListener(onConfirmClickListener);
		return al;
	}

	/**
	 * 
	 * @param mContext
	 * @param width
	 * @param titleStringId
	 * @param cancelStringId
	 * @param comfirmStringId
	 * @param onConfirmClickListener
	 * @return
	 */
	public static AlertDialog showDialog(final Context mContext, int width,
			int titleStringId, int cancelStringId, int comfirmStringId,
			OnClickListener cancelClickListener,
			OnClickListener onConfirmClickListener) {
		al = showDialog(mContext, titleStringId, onConfirmClickListener);
		Window window = al.getWindow();
		TextView cancelBtn = (TextView) window.findViewById(R.id.cancel_btn);
		cancelBtn.setText(mContext.getResources().getString(cancelStringId));
		cancelBtn.setOnClickListener(cancelClickListener);
		TextView comfirmBtn = (TextView) window.findViewById(R.id.comfirm_btn);
		comfirmBtn.setText(mContext.getResources().getString(comfirmStringId));
		return al;
	}

	public static void dismissDailog() {
		if (al != null) {
			al.cancel();
		}
	}

	/***
	 * 
	 * 获取屏幕宽度
	 */
	public static int getWidth(Context mContext) {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		return dm.widthPixels;
		// int screenHeigh = dm.heightPixels;
	}

}
